import { login, logout, getInfo } from '@/api/login'
import {getToken, setToken, removeToken, setLanguage, getLanguage} from '@/utils/auth'

const user = {
  state: {
    language: getLanguage() ? getLanguage() : 's',
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    phonenumber:'',
    nickname:'',
    password:'',
    accountid:'',
    userType:''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_PHONENUMBER: (state, phonenumber) => {
      state.phonenumber = phonenumber
    },
    SET_NICKNAME: (state, nickname) => {
      state.nickname = nickname
    },
    SET_PASSWORD: (state, password) => {
      state.password = password
    },
    SET_ACCOUNTID: (state, accountid) => {
      state.accountid = accountid
    },
    SET_ACCOUNTTYPE: (state, userType) => {
      state.userType = userType
    },
    SET_LANGUAGE: (state, language) => {
      state.language = language
    }
  },

  actions: {
      setLanguage({commit}, language) {
          setLanguage(language);
          commit('SET_LANGUAGE', language)
      },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password.trim()
      const loginType = userInfo.loginType
      const userType = userInfo.userType
      const grant_type = userInfo.grant_type
      const validCode = userInfo.validCode
      return new Promise((resolve, reject) => {
        login(username, password, loginType, userType, validCode, grant_type).then(res => {
          if(res.resp_code == 0){
              setToken(res.datas.access_token)
              commit('SET_TOKEN', res.datas.access_token)

              resolve(0)
          }else{
            resolve(res.resp_msg);
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(res => {
          const user = res.datas;
          const avatar = user.headImgUrl == "" ? require("@/assets/image/profile.jpg") : user.headImgUrl;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.username)
          commit('SET_PHONENUMBER', user.mobile)
          commit('SET_NICKNAME', user.nickname)

          commit('SET_ACCOUNTID', user.id)
          commit('SET_ACCOUNTTYPE' , user.type)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    LogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_NICKNAME', '')
        commit('SET_ACCOUNTID', '')
        commit('SET_ACCOUNTTYPE', '')
        removeToken()
        resolve(0)
      })
    }
  }
}

export default user
